

import sys, bpy, bpy.utils
from bpy.types import Panel, Menu, Operator, PropertyGroup, Operator, AddonPreferences
from bpy.props import *

class OSSL_Preferences(AddonPreferences):
    bl_idname = "ossl"
    bl_label = "OS/SL Addon Development Preferences"
    
    dev_dir_path: StringProperty(
        name="Development Directory",
        subtype='DIR_PATH',
        default='C:\\Blender\\_addons\\OSSL\\',
    )
    
    def get_dev_dir_path(self):
        return self.dev_dir_path # .value()
    
    class OSSL_PT_save_to_dev_dir(Operator):
        bl_idname = "ossl.save_to_dev_dir"
        bl_label = "Save OS/SL Addon to Development Directory"

        def remove_pycache(self):
            import os
            for name in filter(lambda n: n != "__pycache__", os.listdir()):
                try:
                    os.chdir(name)
                    self.remove_pycache()
                    os.chdir("..")
                except: pass
            if "__pycache__" in os.listdir():
                cwd = os.getcwd()
                try:
                    os.chdir("__pycache__")
                    for nn in os.listdir():
                        try: os.remove(nn)
                        except: pass
                except e:
                    print(e)
                    pass
                os.chdir(cwd)
                os.rmdir("__pycache__")
        
#        def write_content_xml(self, f, indent, i):
#            import os, stat
#            import ossl.util as util
#            if i[0] == "d":
#                f.write("\t"*indent + "<d" + "n='" + i[1] + "' t='" + i[2] + "'>\n")
#                chdir(i[1])
#                for ii in util.fs_dirlist(None): self.write_content_xml(f, indent+1, ii)
#                chdir("..")
#                f.write("\t"*indent + "</d>")
#            else:
#                f.write("\t"*indent + "<f" + "n='" + i[1] + "' t='" + i[2] + "'/>\n")
#            
        def execute(self, context):
            import shutil, os, os.path, ossl.util as util
            #from ossl.preferences import OS
            cwd = os.getcwd()
            os.chdir(os.path.dirname(sys.modules["ossl"].__file__))
            self.remove_pycache()
            os.chdir("..")
            f = open("ossl\\addon_contents.xml", "w")
            f.write(util.fs_xml("ossl"))
            f.close
            from datetime import datetime
            s = util.format_utc(datetime.now())
            f = open("ossl\\version_datetime", "w")
            f.write(s)
            f.close()
            s = datetime.now().strftime("%Y-%m-%d %H-%M-%S")
            addon = bpy.context.preferences.addons.get("ossl")
            #print(dir(addon.preferences))
            dirpath = addon.preferences.get_dev_dir_path() # get('dev_dir_path')
            shutil.make_archive(dirpath + "ossl " + s, "zip", ".", "ossl")
            print("saved " + os.getcwd() + "\\ossl to " + dirpath + "ossl " + s)
            os.chdir(cwd)
            return {'FINISHED'}
    
    def draw(self, context):
        self.layout.prop(self, "dev_dir_path")
        self.layout.operator("ossl.save_to_dev_dir")
    
    def register():
        print("registering: OSSL Preferences Properties")
        bpy.utils.register_class(OSSL_Preferences.OSSL_PT_save_to_dev_dir)
        print("registered: OSSL Preferences Properties")

    def unregister():
        print("unregistering: OSSL Preferences Properties")
        bpy.utils.unregister_class(OSSL_Preferences.OSSL_PT_save_to_dev_dir)
        print("unregistered: OSSL Preferences Properties")

def register():
    print("registering: OSSL Preferences")
    bpy.utils.register_class(OSSL_Preferences)
    print("registered: OSSL Preferences")

def unregister():
    print("unregistering: OSSL Preferences")
    bpy.utils.unregister_class(OSSL_Preferences)
    print("unregistered: OSSL Preferences")

if __name__ == "__main__":
    import ossl.util as util
    util.reset_addon()
